// reducer.tsx
import { DataType } from './column';

// 定义 action 类型
export const ACTIONS = {
  ADD_USER: 'add_user',
  DELETE_USER: 'delete_user',
  UPDATE_USER: 'update_user',
  SET_USERS: 'set_users'
};

// 定义 action 类型
type UserAction =
  | { type: typeof ACTIONS.ADD_USER; payload: DataType }
  | { type: typeof ACTIONS.DELETE_USER; payload: string } // key
  | { type: typeof ACTIONS.UPDATE_USER; payload: DataType }
  | { type: typeof ACTIONS.SET_USERS; payload: DataType[] };

// 初始状态
export const initialState: DataType[] = [
  {
    key: '1',
    name: '徐小夕',
    role: 1,
    desc: 'H5-Dooring作者, 掘金签约作者，知乎专栏作家',
    tags: ['前端工程师', 'developer'],
  },
  {
    key: '2',
    name: '张三',
    role: 2,
    desc: '知乎专栏作家',
    tags: ['前端工程师', 'developer' , 'Dooring'],
},
{
    key: '3',
    name: '李盟',
    role: 3,
    desc: 'Dooring共建者',
    tags: ['后端工程师', 'V6.Dooring' , 'Dooring'],
},
{
    key: '4',
    name: '王阿明',
    role: 4,
    desc: '技术合伙人',
    tags: ['全栈工程师', '橙子表单'],
},
{
    key: '5',
    name: '张小明',
    role: 5,
    desc: '技术合伙人',
    tags: ['全栈工程师', '橙子表单'],
},
{
    key: '6',
    name: 'Tom',
    role: 6,
    desc: '技术合伙人',
    tags: ['全栈工程师', 'Next-Admin'],
},
{
    key: '7',
    name: 'Json',
    role: 7,
    desc: '技术合伙人',
    tags: ['全栈工程师', 'Next-Admin'],
},
  // 其他初始数据...
];

// Reducer 函数
export function userReducer(oldinitialState: DataType[], action: UserAction): DataType[] {
        let newList =JSON.parse(JSON.stringify(oldinitialState))
        if(action.type=="addlist"){
            newList.push(action.payload)
        }
        if(action.type=="dellist"){
            newList=newList.filter(d=>{
                return d.key!=action.payload
            })
        }
        if(action.type=="gengxin"){
            const index=newList.findIndex(d=>{
                return d.role==action.payload.role
            })       
            newList[index]=action.payload
        }

    
    return newList
}